package com.hqyj.javaSpringBoot.modules.account.controller;

import com.github.pagehelper.PageInfo;
import com.hqyj.javaSpringBoot.modules.account.entity.Resource;
import com.hqyj.javaSpringBoot.modules.account.service.ResourceService;
import com.hqyj.javaSpringBoot.modules.common.entiy.ResultEntity;
import com.hqyj.javaSpringBoot.modules.common.entiy.SearchBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
public class ResourceController {

    @Autowired
    private ResourceService resourceService;

    /**
     * 127.0.0.1/api/resource
     * {"perssion":"添加"}
     * 添加权限，因为权限具有唯一性，所以先根据权限名称进行一次检索，
     * 如果存在就不在添加，不存在则添加
     * @param resource 权限
     * @return ResultEntity结果和添加的对象
     */
    @PostMapping(value = "/resource",consumes = "application/json")
    public ResultEntity<Resource> insertResource(@RequestBody Resource resource){
        return resourceService.insertResource(resource);
    }

    /**
     * 127.0.0.1/api/{id}     ----id=1
     * 删除权限
     * @param id 权限id
     * @return ResultEntity结果
     */
    @DeleteMapping(value = "/resource/{id}")
    public ResultEntity<Resource> deleteResourceByReourceId(@PathVariable int id){
        return resourceService.deleteResourceByReourceId(id);
    }

    /**
     * 127.0.0.1/api/resource
     * {"id":"2","perssion":"增加"}
     * 修改权限
     * @param resource 权限
     * @return ResultEntity结果和修改后的对象
     */
    @PutMapping("resource")
    ResultEntity<Resource>  updateResourceByReourceId(@RequestBody Resource resource){
        return resourceService.updateResourceByReourceId(resource);
    }

    /**
     * 127.0.0.1/api/{id}     ----id=2
     * 根据id查询权限
     * @param id 权限id
     * @return ResultEntity结果
     */
    @GetMapping("/resource/{id}")
    Resource getResourceByResourceId(@PathVariable int id){
        return resourceService.getResourceByResourceId(id);
    }

    /**
     * 127.0.0.1/api/resources
     *  {"currentPage":1, "pageSize":5, "orderBy":"id",
     *      *      * "direction":"desc", "keyword": "增加"}
     * 模糊+分页查询
     * @param searchBean
     * @return
     */
    @PostMapping(value = "/resources",consumes = "application/json")
    public PageInfo<Resource> getResources(@RequestBody SearchBean searchBean){
        return resourceService.getResources(searchBean);
    }
}
